/**
 * 
 */
package com.Adapters.Items;


import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.Adapters.NotificationAdapter;
import com.Animations.AnimUtil;
import com.Animations.AnimUtil.AnimationCompleted;
import com.Animations.AnimationLists;
import com.Data.Notifications;
import com.Data.Notifications.onDeleteNotification;
import com.Downloaders.ProfilePictureDownloader;
import com.Downloaders.ProfilePictureDownloader.onProfilePictureDownloaded;
import com.Enums.NotificationTypes;
import com.Managers.FragmentManagerClass;
import com.TextFormat.UtilFonts;
import com.UtilDate.UtilDate;
import com.activities.ActivityComments;
import com.activities.ActivityProfileUser;
import com.activities.ImagePagerActivity;
import com.shareup.R;


/**
 * @author Chris
 * 
 */
public class ItemNotification implements ItemPost {

    private NotificationAdapter adapter;

    private boolean animate;

    private Context context;

    private ViewHolder holder;

    private LayoutInflater inflater;

    private Notifications notification;

    class ViewClickListener implements OnClickListener {

        @Override
        public void onClick(View v) {

            {
                showProfilePopUp();
            }
        }
    }

    class GoClickListener implements OnClickListener {

        Notifications extraData;

        public GoClickListener(Notifications not) {

            this.extraData = not;
        }

        @Override
        public void onClick(View v) {

            if (extraData.getExtra() != null
                        && !extraData.getExtra().equals("0")) {

                Intent intent = new Intent();

                int Type = notification.getType();
                Log.e("type", Type + "");
                if (Type == NotificationTypes.COMMENT.getValue())

                {
                    intent.setClass(context, ActivityComments.class);
                    // intent.putExtra(name, value)
                }
                else
                    if (Type == NotificationTypes.LIKE.getValue()) {
                        intent.setClass(context, ImagePagerActivity.class);
                        Bundle extras = new Bundle();
                        extras.putIntArray("IDS", new int[] { Integer.parseInt(extraData.getExtra()) });
                        intent.putExtras(extras);
                        showNotificationIntent(intent);

                    }

            }

        }
    }

    private void showNotificationIntent(Intent intent) {

        context.startActivity(intent);
        AnimUtil.getActivityTransition(FragmentManagerClass.aNotifications, true);
    }

    class ViewHolder {

        public TextView comment;

        public RelativeLayout container;

        public TextView date;

        public ImageView deleteB;

        public TextView name;

        public ImageView profile;
    }

    public ItemNotification(LayoutInflater inflater,
                Notifications notification,
                NotificationAdapter adapter) {

        super();

        this.inflater = inflater;
        this.notification = notification;
        this.context = adapter.getContext();
        this.adapter = adapter;

    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#AnimateIn()
     */
    @Override
    public void AnimateIn() {

        AnimUtil.animateFadeIn(holder.container);

    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#AnimateOut(com.Animations.AnimUtil.
     * AnimationCompleted)
     */
    @Override
    public void AnimateOut(AnimationCompleted callback) {

        AnimUtil.animateFadeOut(holder.container, callback);

    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#clearAnimatonList()
     */
    @Override
    public void clearAnimatonList() {

        // TODO Auto-generated method stub

    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#delete()
     */
    @Override
    public void delete() {

        Notifications not = new Notifications(Notifications.ACTION_DELETE);
        not.setIdNotification(notification.getIdNotification());
        not.setCallbackDelete(new onDeleteNotification() {

            @Override
            public void Completed(String res) {

                Log.e("", res);
                AnimateOut(new AnimationCompleted() {

                    @Override
                    public void Completed() {

                        adapter.remove(notification);
                        holder.container.setVisibility(View.VISIBLE);

                    }
                });

            }
        });
        not.execute();

    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#getCommentsCount()
     */
    @Override
    public int getCommentsCount() {

        // TODO Auto-generated method stub
        return 0;
    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#getLikesCount()
     */
    @Override
    public int getLikesCount() {

        // TODO Auto-generated method stub
        return 0;
    }

    public Notifications getNotification() {

        return notification;
    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#getPostId()
     */
    @Override
    public int getPostId() {

        return notification.getIdNotification();
    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#getPublicationDate()
     */
    @Override
    public String getPublicationDate() {

        return notification.getDate();
    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#getView(android.view.View)
     */
    @Override
    public View getView(View v, int position) {

        if (v == null) {

            v = inflater.inflate(R.layout.item_notification, null);

            holder = new ViewHolder();

            holder.comment = (TextView) v.findViewById(R.id.user_comment);
            holder.name = (TextView) v.findViewById(R.id.user_name);
            holder.container = (RelativeLayout) v.findViewById(R.id.post_container_all);
            holder.date = (TextView) v.findViewById(R.id.user_date);
            holder.profile = (ImageView) v.findViewById(R.id.user_image_profile);
            holder.deleteB = (ImageView) v.findViewById(R.id.delete_button);

            UtilFonts.setTextBoldFont(holder.name);
            UtilFonts.setTextRegularFont(holder.comment, holder.date);

            v.setTag(holder);

        }
        else {

            holder = (ViewHolder) v.getTag();
        }

        if (!AnimationLists.displayedComments.contains(notification.getIdNotification())) {
            holder.container.setVisibility(View.INVISIBLE);
            animate = true;
            AnimationLists.displayedComments.add(notification.getIdNotification());
        }
        else
            animate = false;

        setTexts();

        return v;
    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#getViewType()
     */
    @Override
    public int getViewType() {

        return RowType.NOTIFICATION.ordinal();
    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#setCommentsCount(int)
     */
    @Override
    public void setCommentsCount(int count) {

        // TODO Auto-generated method stub

    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#setLikesCount(int)
     */
    @Override
    public void setLikesCount(int count) {

        // TODO Auto-generated method stub

    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#updatePublicationDate(java.lang.String)
     */
    @Override
    public void updatePublicationDate(String newDate) {

        notification.setDate(newDate);

    }

    private void downloadProfilePicture() {

        if (animate)
            AnimateIn();

        String url = notification.getFrom().getProfilePicture();

        ProfilePictureDownloader pd = new ProfilePictureDownloader();
        pd.setCallbackProfilePicture(new onProfilePictureDownloaded() {

            @Override
            public void completed(Bitmap result) {

                holder.profile.setImageBitmap(result);

            }
        });
        pd.execute(url);

    }

    private void setTexts() {

        String message = "";

        int Type = notification.getType();
        if (Type == NotificationTypes.COMMENT.getValue())
            message = context.getResources().getString(R.string.notification_comment_added);
        else
            if (Type == NotificationTypes.FOLLOWING.getValue())
                message = context.getResources().getString(R.string.notification_following);
            else
                if (Type == NotificationTypes.LIKE.getValue())
                    message = context.getResources().getString(R.string.notification_like_added);
                else
                    if (Type == NotificationTypes.PICTURESHARED.getValue())
                        message = context.getResources().getString(R.string.notification_shared_picture);
                    else
                        if (Type == NotificationTypes.USINGCOVER.getValue())
                            message = context.getResources().getString(R.string.notification_using_cover);
                        else
                            if (Type == NotificationTypes.USINGPROFILE.getValue())
                                message = context.getResources().getString(R.string.notification_using_profile);

        String noty = String.format(message, "");

        holder.name.setText(notification.getFrom().getRealName());
        holder.comment.setText(noty.trim());
        //
        holder.container.setOnClickListener(new GoClickListener(notification));
        holder.name.setOnClickListener(new ViewClickListener());
        holder.profile.setOnClickListener(new ViewClickListener());
        holder.deleteB.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                delete();

            }
        });

        //
        if (notification.getDate() != holder.date.getTag()) {
            holder.date.setText("");
            holder.date.setTag(notification.getDate());
            UtilDate dt = new UtilDate(this.context);
            dt.setTimeDifference(notification.getDate(), holder.date);
        }
        //

        //
        downloadProfilePicture();

    }

    private void showProfilePopUp() {

        Intent intent = new Intent(context, ActivityProfileUser.class);
        intent.putExtra("imgprofile", notification.getFrom().getProfilePicture());
        intent.putExtra("nameprofile", notification.getFrom().getRealName());
        intent.putExtra("IDUSER", notification.getFrom().getID());
        //
        context.startActivity(intent);
        AnimUtil.getActivityTransition(FragmentManagerClass.aNotifications, true);
    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#getPosition()
     */
    @Override
    public int getPosition() {

        // TODO Auto-generated method stub
        return 0;
    }

}
